//
//  HMDrugStencil.swift
//  HealthMonitoring
//
//  Created by YTYanK on 02/04/2019.
//  Copyright © 2019 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON

/**
 *  模版
 */
class HMDrugStencil: HMViewController {
    // cell 高度
    var cellHeight: CGFloat = 0
    /// 用户类型
    var dataType: DataType!
    /// 数据列表
    var dataList = Array<Any>()
    
    /// 列表
    private lazy var table: UITableView = {
        let _table = UITableView()
        _table.delegate = self
        _table.dataSource = self
        _table.isHidden = true
        _table.register(HMDrugStencilCell.classForCoder(), forCellReuseIdentifier: "HMDrugStencilCell")
        view.addSubview(_table)
        return _table
    }()
    
    /// 空数据背景
    private lazy var subViewNullData: UIView = {
        let subView = UIView.creatoreBgNullDataView(frame: CGRect.zero)
        view.addSubview(subView)
        return subView
    }()
    
    override func setupView() {
        view.backgroundColor = configure.shareConfigure.HMColor2
        
        self.subViewNullData.centerXYAlign(view: view)
        
        self.table.topVertically(view: view, predicate: "\(10)")
        self.table.heightConstrain(predicate: "\(view.frame.height)")
        self.table.leadingAlign(view: view, predicate: "0")
        self.table.trailingAlign(view: view, predicate: "0")
//        self.table.bottomAlign(view: view, predicate: "-44")
        self.updateTable(self.dataType!)
    }

    
    
    
    func updateTable(_ dataType: DataType) {
        NetRequestTool.shareInstance.met = .get
        var url = ""
        var par = Dictionary<String,Any>()
        
        if dataType == .TypeVisitDoctor {
            url = "user/medicine/suggestion-brief"
            par = ["type" : "0"]
        }else {
            url = "doctor/doc/presc-list"
            par = [:]
            // 医生获取处方列表
        }
        
        NetRequestTool.request(url: url, parameters: par, success: { (json) in
      
            let code = json["code"].intValue
            if code == 0 {
                self.dataList = json["data"].arrayValue
                if self.dataList.count > 0 {
                    self.table.isHidden = false
                    self.table.reloadData()
                   
                }
            }else {
                //其他错误
                self.table.isHidden = true
            }
        }, fail: { (code, err) in
            print("报错:\(code)  err: -> \(err)")
        }) {}
    
}


}




extension HMDrugStencil: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataList.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.cellHeight
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var dataJson =  JSON.init(self.dataList)
        let data = (dataJson[indexPath.row].dictionary)!
        var dateStr = String.chineseTime(frome: (data["lastModifiedDate"]?.stringValue)!)
        dateStr = String(dateStr.prefix(dateStr.count - 3))
        
        let adviseAry: JSON = ["describe" : (data["applyIllness"]?.stringValue)!]
        
//        let adviceLife: JSON = []
        
//        var moreName = ""
//        var btnName = ""
//        if self.titleSign == "用药建议" {
//            btnName = "添加提醒"
//            moreName = "点击查看更多"
//        }else {
//            btnName = "发送患者"
//            moreName = "编辑或点击查看更多"
//        }
        
        let cell = HMDrugStencilCell.initCell(dataType: self.dataType, json: (adviseAry,adviseAry,adviseAry), tableView: tableView, indexPath: indexPath)
        
        self.cellHeight = (cell?.cellHeight)!
        return cell!
    }

}
